package com.jd.fruit.controller.admin.shop;

import java.io.IOException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.common.util.DictUtil;
import com.jd.fruit.model.admin.shop.Attribute;
import com.jd.fruit.model.admin.shop.Category;
import com.jd.fruit.model.admin.shop.Goods;
import com.jd.fruit.model.admin.shop.SetShop;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
/**
 * 店铺管理设置
 * @author admin
 *
 */
@Controller("/admin/shop/setshop")
public class SetShopController extends BaseController{

	public void index(){
		render("shop_list.html");
	}
	/**
	 * 店铺信息
	 */
	public void list(){
		int pageNumber = this.getParaToInt(0,1);
		Page<SetShop> shoplist = SetShop.dao.list(pageNumber,DictUtil.default_pageSize);
		this.getRequest().setAttribute("shoplists", shoplist);
		render("shop_list.html");
	}
	/**
	 * 删除店铺信息
	 * 
	 * @throws IOException
	 */
	public void delete() throws IOException {
		String id = this.getPara("id");
		JSONObject result = new JSONObject();
		boolean isOk = SetShop.dao.deleteById(id);
		if (isOk) {// 删除成功
			this.renderText(DictUtil.success);
			result.put("code", DictUtil.success);
		} else { // 删除失败
			this.renderText(DictUtil.failed);
			result.put("code", DictUtil.failed);
		}
		this.renderJson(result);
	}
	/**
	 * 编辑
	 */
	public void edit(){
		String id = this.getPara(0);
		SetShop shop =getModel(SetShop.class);
		 shop = SetShop.dao.findBy(id);
		setAttr("shops", shop);
		render("shop_edit.html");
	}
	
	/**
	 * 更新
	 */
	public void save(){
		boolean isflg=false;
		String id =getPara("id");
		String website=getPara("website");
		String shopname=getPara("shopname");
		String shopannouncement=getPara("shopannouncement");
		String micro_signal=getPara("micro_signal");
		String public_no=getPara("public_no");
		String qq=getPara("qq");
		String machine_no=getPara("machine_no");
		String iphone_no=getPara("iphone_no");
		String shop_image=getPara("shop_image");
		String shop_background=getPara("shop_background");
		try {
		//存放数据
		SetShop shop =new SetShop();
		//保存的数据id如果不是空则是更新 否则 保存
		if(StrKit.notBlank(id)){
			shop.set("id", id);
			shop.set("website", website);
			shop.set("shopname", shopname);
			shop.set("shopannouncement", shopannouncement);
			shop.set("micro_signal", micro_signal);
			shop.set("qq", qq);
			shop.set("machine_no", machine_no);
			shop.set("public_no", public_no);
			shop.set("iphone_no", iphone_no);
			shop.set("shop_image", shop_image);
			shop.set("shop_background", shop_background);
			shop.update();
		  //如果无更新的数据并且没有报错则视为成功
			isflg=true;
		}else{
			SetShop setshop = getModel(SetShop.class);
//			isflg=SetShop.dao.save(setshop);
		}
		}catch (Exception e) {
			// TODO: handle exception
			
		}
	}
	/**
	 * 加载商品分类属性Jason数据
	 * 
	 * @throws IOException
	 */
	public void loadData() {
		String iphone_no = getPara("iphone_no");
		String result = "";
		JSONArray jsarray = null;
		if (!StrKit.isBlank(iphone_no) && iphone_no.equals(DictUtil.jsontree)) {
			jsarray = SetShop.dao.findById(iphone_no);
			if (jsarray != null) {
				result = jsarray.toJSONString();
			}
		} else {
			result = Category.dao.loadTreeGrid();
		}
		renderJson(result);
	}
}
